package com.acerola.yuan_back.controller;

import com.acerola.yuan_back.common.Result;
import com.acerola.yuan_back.dao.WishMapper;
import com.acerola.yuan_back.pojo.Wish;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: yuan_back
 * @description: 心愿控制层
 * @author: Acerola
 * @create: 2022-07-30 20:56
 **/
@Api(tags = "心愿控制层")
@CrossOrigin
@RestController
@RequestMapping("/wish")
public class WishController {

    @Autowired
    private WishMapper wishMapper;

    @ApiOperation("列出所有心愿")
    @GetMapping("/list")
    private Result<?> list(){
        QueryWrapper<Wish> wishQueryWrapper = new QueryWrapper<>();
        wishQueryWrapper.select().orderByDesc("wish_time");
        return new Result<>().success().put(wishMapper.selectList(wishQueryWrapper));
    }
    @ApiOperation("添加心愿")
    @PostMapping("/add")
    private Result<?> add(@RequestBody Wish wish){
        wish.setIsDone(0);
        Integer save = wishMapper.insert(wish);
        if (save!=0){
            return new Result<>().success("添加成功");
        }else {
            return new Result<>().error("添加失败");
        }
    }

    @ApiOperation("移除心愿")
    @PostMapping("/remove")
    private Result<?> remove(Integer id){
        Wish wish = wishMapper.selectById(id);
        wish.setIsDone(1);
        Integer b = wishMapper.updateById(wish);
        if (b!=0){
            return new Result<>().success("修改成功");
        }else {
            return new Result<>().error("修改失败");
        }
    }
}
